package org.dubbo.live.common.interfaces.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.dubbo.live.user.dto.UserDTO;
import org.dubbo.live.user.interfaces.IUserRpc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: //todo
 * @Author: 张广焱
 * @Date: 2024-03-24 9:03
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @DubboReference
    private IUserRpc userRpc;

    @GetMapping("/getUserInfo")
    public String getUserInfo(Long userid) {
        return userRpc.getByUserId(userid).toString();
    }

    @GetMapping("/updateUserInfo")
    public boolean getUserInfo(Long userid, String nickname) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userid);
        userDTO.setNickName(nickname);
        return userRpc.updateUserInfo(userDTO);
    }


    @GetMapping("/insertOne")
    public boolean insertOne(Long userid) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userid);
        userDTO.setNickName("idea-test");
        userDTO.setSex(1);
        return userRpc.insertOne(userDTO);
    }

    @GetMapping("/batchQueryUserInfo")
    public Map<Long, UserDTO> batchQueryUserInfo(String userIdStr) {
        return userRpc.batchQueryUserInfo(Arrays.asList(userIdStr.split(",")).stream().map(x -> Long.parseLong(x)).collect(Collectors.toList()));
    }
}
